package com.tengju.data.domain.shared;

import com.tengju.data.domain.model.clickhouse.dashboard.MonthQuarterEnum;
import org.apache.commons.lang3.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.stream.Collectors;


public class DateUtil {

    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_TIME_FORMAT_FILE_NAME = "MM月dd日HH点mm分";
    public static final String DATE_TIME_MINUTE_FORMAT = "yyyy-MM-dd HH:mm";
    public static final String DATE_DAY_FORMAT = "yyyy-MM-dd";
    public static final String DATE_MONTH_FORMAT = "yyyy-MM";
    public static final String DATE_CHINESE_FORMAT = "yyyy年MM月dd日";
    public static final String DATE_FORMAT = "yyyyMMdd";
    public static final String DATE_FORMAT_MONTH_INT = "yyyyMM";
    public static final String DATE_YEAR_FORMAT = "yyyy";
    public static final String DATE_FORMAT_MMDD = "MM/dd";
    public static final String DATE_FORMAT_FFFF = "yyyyMMddHHmmssffff";


    private DateUtil() {
        //
    }


    /**
     * 将 2021-03-01 转换为 03/01
     * @param dateTime String 类型的日期 2021-03-01
     */
    public static String parseStringToMMDD(String dateTime){
        try {
            LocalDate localDate = DateUtil.formatToLocalDate(dateTime, DateUtil.DATE_DAY_FORMAT);
            String truncateDate = StringUtils.truncate(String.valueOf(localDate), 5, 5);
            return StringUtils.replace(truncateDate, "-", "/");
        }catch (Exception e){
            throw new DomainException(DomainExceptionCode.STRING_NOT_FORMAT_TO_DATE);
        }
    }

    public static LocalDateTime parseStringToLocalDateTime(String time, String format) {
        if (StringUtils.isBlank(time)) {
            return null;
        }
        return LocalDateTime.parse(time,DateTimeFormatter.ofPattern(format));
    }

    public static LocalDate parseStringToLocalDate(String time, String format) {
        if (StringUtils.isBlank(time)) {
            return null;
        }
        return LocalDate.parse(time,DateTimeFormatter.ofPattern(format));
    }

    public static Date parseStringToDate(String format, String time) {
        try {
            if (StringUtils.isBlank(time)) {
                return null;
            }
            SimpleDateFormat dateFormat = new SimpleDateFormat(format);
            return dateFormat.parse(time);
        } catch (ParseException e) {
            throw new DateParseException(format, time);
        }
    }

    public static String parseDateToString(String format, Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static Date formatDate(String format, Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return parseStringToDate(format, sdf.format(date));
    }

    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static LocalDateTime
    dateToLocalDateTime(Date date) {
        if (date == null) {
            return null;
        }
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    public static LocalDate dateToLocalDate(Date date) {
        if (date == null) {
            return null;
        }
        return LocalDate.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    public static String formatLocalDateTime(String format, LocalDateTime localDateTime) {
        return localDateTime.format(DateTimeFormatter.ofPattern(format));
    }

    public static String formatLocalDate(String format, LocalDate localDate) {
        return localDate.format(DateTimeFormatter.ofPattern(format));
    }

    /**
     * 前/后?天
     */
    public static Date rollDay(Date d, int day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.add(Calendar.DAY_OF_MONTH, day);
        return cal.getTime();
    }

    public static Integer formatMonthInt(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return null;
        }
        String monthStr = localDateTime.format(DateTimeFormatter.ofPattern(DATE_FORMAT_MONTH_INT));
        return CastUtil.castInt(monthStr);
    }

    public static Integer formatLastMonthInt(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return null;
        }
        LocalDateTime lastMonth = localDateTime.minusMonths(1);
        String monthStr = lastMonth.format(DateTimeFormatter.ofPattern(DATE_FORMAT_MONTH_INT));
        return CastUtil.castInt(monthStr);
    }

    public static Integer formatLastQuarterInt(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return null;
        }
        int monthValue = localDateTime.getMonthValue();
        switch (monthValue) {
            case 1:
            case 2:
            case 3:
                return Integer.parseInt(localDateTime.getYear() - 1 + "4");
            case 4:
            case 5:
            case 6:
                return Integer.parseInt(localDateTime.getYear() + "1");
            case 7:
            case 8:
            case 9:
                return Integer.parseInt(localDateTime.getYear() + "2");
            case 10:
            case 11:
            case 12:
                return Integer.parseInt(localDateTime.getYear() + "3");
            default:
                return null;
        }
    }


    public static Integer formatCurrentQuarterInt(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return null;
        }
        int monthValue = localDateTime.getMonthValue();
        switch (monthValue) {
            case 1:
            case 2:
            case 3:
                return Integer.parseInt(localDateTime.getYear() + "1");
            case 4:
            case 5:
            case 6:
                return Integer.parseInt(localDateTime.getYear() + "2");
            case 7:
            case 8:
            case 9:
                return Integer.parseInt(localDateTime.getYear() + "3");
            case 10:
            case 11:
            case 12:
                return Integer.parseInt(localDateTime.getYear() + "4");
            default:
                return null;
        }
    }

    public static void main(String[] args) {
        System.out.println(formatLastQuarterInt(LocalDateTime.now()));
    }

    public static Integer formatBeforeTwoMonthInt(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return null;
        }
        LocalDateTime lastMonth = localDateTime.minusMonths(2);
        String monthStr = lastMonth.format(DateTimeFormatter.ofPattern(DATE_FORMAT_MONTH_INT));
        return CastUtil.castInt(monthStr);
    }

    public static String formatMonthToStr(Integer month) {
        Date date = parseStringToDate(DATE_FORMAT_MONTH_INT, month + "");
        return parseDateToString(DATE_MONTH_FORMAT, date);
    }

    public static Integer formatDayInt(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return null;
        }
        String monthStr = localDateTime.format(DateTimeFormatter.ofPattern(DATE_FORMAT));
        return CastUtil.castInt(monthStr);
    }

    public static String formatDayToStr(Integer day) {
        Date date = parseStringToDate(DATE_FORMAT, day + "");
        return parseDateToString(DATE_DAY_FORMAT, date);
    }

    /**
     * 获取当月第一天的最小时间
     *
     * @return 当月第一天的最小时间
     */
    public static LocalDateTime getFirstDayOfMonthMin() {
        LocalDateTime date = LocalDateTime.now();
        LocalDateTime firstDay = date.with(TemporalAdjusters.firstDayOfMonth());
        return LocalDateTime.of(firstDay.toLocalDate(), LocalTime.MIN);
    }

    /**
     * 获取某个月的最小时间
     *
     * @return 获取某个月的最小时间
     */
    public static LocalDateTime getMonthMinOfMonth(LocalDateTime localDateTime) {
        LocalDateTime firstDay = localDateTime.with(TemporalAdjusters.firstDayOfMonth());
        return LocalDateTime.of(firstDay.toLocalDate(), LocalTime.MIN);
    }

    /**
     * 获取下个月的最小时间
     *
     * @return 获取下个月的最小时间
     */
    public static LocalDateTime getNextMonthMinOfMonth(LocalDateTime localDateTime) {
        LocalDateTime nextMonth = localDateTime.plusMonths(1);
        return LocalDateTime.of(nextMonth.toLocalDate(), LocalTime.MIN);
    }

    /**
     * 获取某天的最小时间
     *
     * @return 获取某天的最小时间
     */
    public static LocalDateTime getLocalDateOfDayMin(LocalDate localDate) {
        return LocalDateTime.of(localDate, LocalTime.MIN);
    }

    /**
     * 获取某天的最大时间
     *
     * @return 获取某天的最大时间
     */
    public static LocalDateTime getLocalDateOfDayMax(LocalDate localDate) {
        return LocalDateTime.of(localDate, LocalTime.MAX);
    }

    /**
     * 获取当月第后天的最大时间
     *
     * @return 当月第后天的最大时间
     */
    public static LocalDateTime getLastDayOfMonthMax() {
        LocalDateTime date = LocalDateTime.now();
        LocalDateTime lastDay = date.with(TemporalAdjusters.lastDayOfMonth());
        return LocalDateTime.of(lastDay.toLocalDate(), LocalTime.MAX);
    }

    /**
     * 获取当前时间几个月后的最小时间
     *
     * @param monthsToAdd 几个月后
     * @return 当前时间几个月后的最小时间
     */
    public static LocalDateTime getPlusMonthsMin(int monthsToAdd) {
        LocalDate localDate = LocalDate.now().withDayOfMonth(1);
        LocalDate plusMonths = localDate.plusMonths(monthsToAdd);
        return LocalDateTime.of(plusMonths, LocalTime.MIN);
    }

    /**
     * 获取当前时间几个月前的最小时间
     *
     * @param monthsToSubtract 几个月前
     * @return 当前时间几个月前的最小时间
     */
    public static LocalDateTime getMinusMonthsMin(int monthsToSubtract) {
        LocalDate localDate = LocalDate.now().withDayOfMonth(1);
        LocalDate minusMonths = localDate.minusMonths(monthsToSubtract);
        return LocalDateTime.of(minusMonths, LocalTime.MIN);
    }

    /**
     * 获取月度最后一天
     */
    public static String getLastMonthDay(LocalDateTime localDateTime) {
        LocalDateTime lastDay = localDateTime.with(TemporalAdjusters.lastDayOfMonth());
        return formatVisitDate(lastDay);
    }

    /**
     * 解析日期默认格式  yyyy-MM-dd
     */
    public static String formatVisitDate(LocalDateTime localDateTime) {
        return formatLocalDateTime(DATE_DAY_FORMAT, localDateTime);
    }

    /**
     * 解析日期默认格式
     */
    public static LocalDateTime format(String dateTime, String pattern) {
        return dateToLocalDateTime(parseStringToDate(pattern, dateTime));
    }

    /**
     * 解析日期默认格式
     */
    public static LocalDate formatToLocalDate(String dateTime, String pattern) {
        return dateToLocalDate(parseStringToDate(pattern, dateTime));
    }

    public static LocalDateTime formatToLocalDateTime(String dateTime, String pattern) {
        return dateToLocalDateTime(parseStringToDate(pattern, dateTime));
    }

    /**
     * 获取季度的最小时间 如：参数 2021Q2 返回 2021-04-01T00:00
     *
     * @param dateStr 如：2021Q2
     * @return 季度的最小时间
     */
    public static LocalDateTime getQuarterMin(String dateStr) {
        if (StringUtils.isBlank(dateStr) || dateStr.length() != 6) {
            throw new DateParseException(dateStr, dateStr);
        }
        String year = dateStr.substring(0, 4);
        String quarter = dateStr.substring(4, 6);
        String minMonthsOfQuarter = getMinMonthsOfQuarter(quarter);
        String[] split = minMonthsOfQuarter.split("-");
        LocalDate of = LocalDate
                .of(Integer.parseInt(year), Integer.parseInt(split[0]), Integer.parseInt(split[1]));
        return LocalDateTime.of(of, LocalTime.MIN);
    }

    /**
     * 获取下个季度的最小时间 如：参数 2021Q4 返回 2022-01-01T00:00
     *
     * @param dateStr 如：2021Q4
     * @return 获取下个季度的最小时间
     */
    public static LocalDateTime getNextQuarterMin(String dateStr) {
        if (StringUtils.isBlank(dateStr) || dateStr.length() != 6) {
            throw new DateParseException(dateStr, dateStr);
        }
        LocalDateTime quarterMax = getQuarterMax(dateStr);
        LocalDateTime localDateTime = quarterMax.plusDays(1);
        return LocalDateTime.of(localDateTime.toLocalDate(), LocalTime.MIN);
    }


    /**
     * 获取季度的最大时间 如：参数 2021Q1 返回 2021-03-31 23:59:59
     *
     * @param dateStr 如：2021Q1
     * @return 季度的最大时间
     */
    public static LocalDateTime getQuarterMax(String dateStr) {
        if (StringUtils.isBlank(dateStr) || dateStr.length() != 6) {
            throw new DateParseException(dateStr, dateStr);
        }
        String year = dateStr.substring(0, 4);
        String quarter = dateStr.substring(4, 6);
        String maxMonthsOfQuarter = getMaxMonthsOfQuarter(quarter);
        String[] split = maxMonthsOfQuarter.split("-");
        LocalDate of = LocalDate
                .of(Integer.parseInt(year), Integer.parseInt(split[0]), Integer.parseInt(split[1]));
        return LocalDateTime.of(of, LocalTime.MAX);
    }

    public static String getMinMonthsOfQuarter(String quarter) {
        String result = "";
        switch (quarter.toUpperCase()) {
            case "Q1":
                result = "01-01";
                break;
            case "Q2":
                result = "04-01";
                break;
            case "Q3":
                result = "07-01";
                break;
            case "Q4":
                result = "10-01";
                break;
            default:
                throw new DateParseException(quarter, quarter);
        }
        return result;
    }

    public static String getMaxMonthsOfQuarter(String quarter) {
        String result = "";
        switch (quarter.toUpperCase()) {
            case "Q1":
                result = "03-31";
                break;
            case "Q2":
                result = "06-30";
                break;
            case "Q3":
                result = "09-30";
                break;
            case "Q4":
                result = "12-31";
                break;
            default:
                throw new DateParseException(quarter, quarter);
        }
        return result;
    }

    /**
     * 获取季度的月份列表 如：参数 2021Q1 返回 [202101,202102,202103]
     *
     * @param dateStr 如：2021Q1
     * @return 季度的月份列表
     */
    public static List<Integer> getYearMonthListOfQuarter(String dateStr) {
        if (StringUtils.isBlank(dateStr) || dateStr.length() != 6) {
            throw new DateParseException(dateStr, dateStr);
        }
        String year = dateStr.substring(0, 4);
        String quarter = dateStr.substring(4, 6);
        List<String> monthStrList = getMonthsOfQuarter(quarter);
        return monthStrList.stream()
                .map(monthStr -> Integer.parseInt(year + monthStr))
                .collect(Collectors.toList());
    }

    public static List<String> getMonthsOfQuarter(String quarter) {
        switch (quarter.toUpperCase()) {
            case "Q1":
                return List.of("01", "02", "03");
            case "Q2":
                return List.of("04", "05", "06");
            case "Q3":
                return List.of("07", "08", "09");
            case "Q4":
                return List.of("10", "11", "12");
            default:
                throw new DateParseException(quarter, quarter);
        }
    }

    public static Integer getMonthOfQuarter() {
        String now = formatLocalDateTime(DATE_MONTH_FORMAT, LocalDateTime.now());
        String[] split = now.split("-");
        switch (split[1]) {
            case "01":
            case "04":
            case "07":
            case "10":
                return 1;
            case "02":
            case "05":
            case "08":
            case "11":
                return 2;
            case "03":
            case "06":
            case "09":
            case "12":
                return 3;
            default:
                throw new DateParseException();
        }
    }

    public static List<String> getQuarterList() {
        List<String> result = new ArrayList<>();
        List<String> monthsList = new ArrayList<>();
        LocalDate localDate = LocalDate.now();
        monthsList.add(formatLocalDate(DATE_FORMAT_MONTH_INT, localDate));
        while (true) {
            localDate = localDate.minusMonths(1);
            monthsList.add(formatLocalDate(DATE_FORMAT_MONTH_INT, localDate));
            if ("202101".equals(formatLocalDate(DATE_FORMAT_MONTH_INT, localDate))) {
                break;
            }
        }
        for (String month : monthsList) {
            String quarter = getYearQuarterByYearMonth(month);
            if (!result.contains(quarter)) {
                result.add(quarter);
            }
        }
        return result;
    }

    public static String getYearQuarterByYearMonth(String months) {
        String year = months.substring(0, 4);
        String month = months.substring(4, 6);
        return year + getQuarterByMonth(month);
    }

    private static String getQuarterByMonth(String month) {
        switch (month) {
            case "01":
            case "02":
            case "03":
                return "Q1";
            case "04":
            case "05":
            case "06":
                return "Q2";
            case "07":
            case "08":
            case "09":
                return "Q3";
            case "10":
            case "11":
            case "12":
                return "Q4";
            default:
                throw new DateParseException();
        }
    }


//    public static void main(String[] args) {
//        LocalDate localDate = LocalDate.now().withDayOfMonth(1);
//        LocalDateTime start = LocalDateTime.of(localDate, LocalTime.MIN);
//        LocalDateTime max = LocalDateTime.of(localDate, LocalTime.MAX);
//        LocalDate nextMonth = localDate.plusMonths(1);
//        LocalDateTime end = LocalDateTime.of(nextMonth, LocalTime.MIN);
//        String minStr = DateUtil
//                .formatLocalDateTime(DateUtil.DATE_TIME_FORMAT, DateUtil.getFirstDayOfMonthMin());
//        String maxStr = DateUtil
//                .formatLocalDateTime(DateUtil.DATE_TIME_FORMAT, DateUtil.getLastDayOfMonthMax());
//
//        Date startTime = DateUtil.localDateTimeToDate(DateUtil.getFirstDayOfMonthMin());
//        Date endTime = DateUtil.localDateTimeToDate(DateUtil.getLastDayOfMonthMax());
//        Date plus = DateUtil.localDateTimeToDate(DateUtil.getPlusMonthsMin(3));
//        LocalDateTime quarterMin = getQuarterMin("2020Q1");
//        LocalDateTime quarterMax = getQuarterMax("2021Q1");
//
//        System.out.println("start:" + start);
//        System.out.println("max:" + max);
//        System.out.println("end:" + end);
//        System.out.println("minStr :" + minStr);
//        System.out.println("maxStr :" + maxStr);
//        System.out.println("startTime :" + startTime);
//        System.out.println("endTime :" + endTime);
//        System.out.println("plus :" + plus);
//        System.out.println("quarterMin :" + DateUtil
//                .formatLocalDateTime(DateUtil.DATE_TIME_FORMAT, quarterMin));
//        System.out.println("quarterMax :" + DateUtil
//                .formatLocalDateTime(DateUtil.DATE_TIME_FORMAT, quarterMax));
//        System.out.println(JsonUtil.toJSONString(getYearMonthListOfQuarter("2021q3")));
//        System.out.println(getMonthOfQuarter());
//        System.out.println(JsonUtil.toJSONString(getQuarterList()));
//        System.out.println(getFirstDateOfQuarter(LocalDate.of(2021, 2, 10)));
//        System.out.println(formatDayInt(format("2021-04-10", DATE_DAY_FORMAT)));
//    }

    /**
     * 取开始日期和结束日期中间的日期
     * 包含开始日期和结束日期
     *
     * @param startDate
     * @param stopDate
     * @return
     */
    public static List<LocalDate> subDate(LocalDate startDate, LocalDate stopDate) {
        if (startDate == null || stopDate == null) {
            return Collections.emptyList();
        }
        List<LocalDate> result = new ArrayList<>();
        LocalDate nextDate = stopDate;
        while (!nextDate.isEqual(startDate)) {
            result.add(nextDate);
            nextDate = nextDate.minusDays(1);
        }
        result.add(startDate);
        return result;
    }

    /**
     * 取开始日期和结束日期中间的月度数据
     * 包含开始日期和结束日期
     *
     * @param startDate
     * @param stopDate
     * @return
     */
    public static List<Month> subDateMonth(LocalDate startDate, LocalDate stopDate) {
        if (startDate == null || stopDate == null) {
            return Collections.emptyList();
        }
        List<Month> result = new ArrayList<>();
        Month nextMonth = new Month(stopDate);
        Month startMonth = new Month(startDate);
        while (!nextMonth.equals(startMonth)) {
            result.add(nextMonth);
            nextMonth = new Month(nextMonth.getRandomDate().minusMonths(1));
        }
        result.add(startMonth);
        return result;
    }

    public static LocalDate getFirstDateOfQuarter(LocalDate date) {
        if (date == null) {
            return null;
        }
        switch (date.getMonthValue()) {
            case 1:
            case 2:
            case 3:
                return LocalDate.of(date.getYear(), 1, 1);
            case 4:
            case 5:
            case 6:
                return LocalDate.of(date.getYear(), 4, 1);
            case 7:
            case 8:
            case 9:
                return LocalDate.of(date.getYear(), 7, 1);
            case 10:
            case 11:
            case 12:
                return LocalDate.of(date.getYear(), 10, 1);
        }
        return null;
    }


    /**
     * 距离当前时长
     */
    public static String getDuration(LocalDateTime time) {
        LocalDateTime now = LocalDateTime.now();
        if (time.getYear() < now.getYear()) {
            return now.getYear() - time.getYear() + "年前";
        } else if (time.getMonthValue() < now.getMonthValue()) {
            return now.getMonthValue() - time.getMonthValue() + "月前";
        } else if (time.getDayOfMonth() < now.getDayOfMonth()) {
            return now.getDayOfMonth() - time.getDayOfMonth() + "天前";
        } else if (time.getHour() < now.getHour()) {
            return now.getHour() - time.getHour() + "小时前";
        } else if (time.getMinute() < now.getMinute()) {
            return now.getMinute() - time.getMinute() + "分钟前";
        } else if (time.getSecond() < now.getSecond()) {
            return now.getSecond() - time.getSecond() + "秒前";
        }
        return "刚刚";
    }

    public static Long toLong(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return 0L;
        }
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    public static List<Date> getBetweenDates(Date start, Date end) {
        if (!start.before(end)) {
            return new ArrayList<>();
        }
        List<Date> result = new ArrayList<Date>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(start);
        tempStart.add(Calendar.DAY_OF_YEAR, 1);

        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end);
        while (tempStart.before(tempEnd)) {
            result.add(tempStart.getTime());
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        return result;
    }

    public static List<LocalDate> getDayIntBetween(LocalDateTime start, LocalDateTime end) {
        List<LocalDate> result = new ArrayList<>();
        result.add(start.toLocalDate());
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(localDateTimeToDate(start));
        tempStart.add(Calendar.DAY_OF_YEAR, 1);

        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(localDateTimeToDate(end));
        while (tempStart.before(tempEnd)) {
            result.add(dateToLocalDate(tempStart.getTime()));
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        result.add(end.toLocalDate());
        return result;
    }

    public static List<String> getDays(String startTime, String endTime) {
        // 返回的日期集合
        List<String> days = new ArrayList<String>();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);
            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);
            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

    /**
     * 获取当前年月日 0000-00-00
     *
     * @param monthQuarterEnum
     * @return java.lang.String
     * @date 2021/5/11 15:49
     */
    public static String getStartMonth(MonthQuarterEnum monthQuarterEnum, LocalDate localDate) {
        String result = StringUtils.EMPTY;
        if (MonthQuarterEnum.CURRENT_MONTH == monthQuarterEnum) {
            result = getCurrentLastDate(localDate);
        }
        if (MonthQuarterEnum.LAST_MONTH == monthQuarterEnum) {
            result = new Month(localDate.minusMonths(1)).getEndDate().toString();
        }
        if (MonthQuarterEnum.CURRENT_QUARTER == monthQuarterEnum) {
            result = new Month(new Quarter(localDate).getStartDate()).getEndDate().toString();
        }
        return result;
    }

    /**
     * 获取当前季度结束年月日 0000-00-00
     *
     * @param monthQuarterEnum
     * @return java.lang.String
     * @date 2021/5/11 15:49
     */
    public static String getEndMonth(MonthQuarterEnum monthQuarterEnum, LocalDate localDate) {
        String result = StringUtils.EMPTY;
        if (MonthQuarterEnum.CURRENT_MONTH == monthQuarterEnum) {
            result = getCurrentLastDate(localDate);
        }
        if (MonthQuarterEnum.LAST_MONTH == monthQuarterEnum) {
            result = new Month(localDate.minusMonths(1)).getEndDate().toString();
        }
        if (MonthQuarterEnum.CURRENT_QUARTER == monthQuarterEnum) {
            result = new Month(new Quarter(localDate).getEndDate()).getEndDate().toString();
        }


        return result;
    }

    //TODO 获取日期重构
    public static LocalDate getEndMonthDate(MonthQuarterEnum monthQuarterEnum, LocalDate localDate) {
        LocalDate result = null;
        if (MonthQuarterEnum.CURRENT_MONTH == monthQuarterEnum) {
            return localDate;
        }
        if (MonthQuarterEnum.LAST_MONTH == monthQuarterEnum) {
            return new Month(localDate).preMonth().getEndDate();
        }
        if (MonthQuarterEnum.CURRENT_QUARTER == monthQuarterEnum) {
            return new Quarter(localDate).getEndDate();
        }
        return result;
    }



    /**
     * 获取当前季度第二个月年月日 0000-00-00
     *
     * @param monthQuarterEnum
     * @return java.lang.String
     * @date 2021/5/11 15:49
     */
    public static String getMediumMonth(MonthQuarterEnum monthQuarterEnum, LocalDate localDate) {
        String result = StringUtils.EMPTY;
        if (MonthQuarterEnum.CURRENT_MONTH == monthQuarterEnum) {
            result = getCurrentLastDate(localDate);
        }
        if (MonthQuarterEnum.LAST_MONTH == monthQuarterEnum) {
            result = new Month(localDate.minusMonths(1)).getEndDate().toString();
        }
        if (MonthQuarterEnum.CURRENT_QUARTER == monthQuarterEnum) {
            result = new Month(new Quarter(localDate).getEndDate().minusMonths(1)).getEndDate().toString();
        }


        return result;
    }

    //获取当前的上一天时间
    public static String getCurrentLastDate(LocalDate localDate) {
        if (localDate == null) {
            return "";
        }
        return localDate.minusDays(1).toString();
    }

    public static LocalDateTime parseBySecond(long seconds) {
        return LocalDateTime.ofInstant(Instant.ofEpochSecond(seconds), ZoneId.systemDefault());
    }

    public static LocalDateTime parseByMilliSeconds(long milliSeconds) {
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(milliSeconds), ZoneId.systemDefault());
    }

    public static int formatDayInt(LocalDate date) {
        if (date == null) {
            return 0;
        }
        return CastUtil.castInt(date.format(DateTimeFormatter.ofPattern(DATE_FORMAT)));
    }

    /**
     *  2021-10-01   ->  10月1日（周五）
     */
    public static String formatLocalDate(LocalDate localDate) {
        if (localDate == null) {
            return "";
        }
        int monthValue = localDate.getMonthValue();
        int dayValue = localDate.getDayOfMonth();
        String currentDate = monthValue + "月" + dayValue + "日";
        DayOfWeek dayOfWeek = localDate.getDayOfWeek();
        switch (dayOfWeek) {
            case MONDAY:
                return currentDate + "(周一)";
            case TUESDAY:
                return currentDate + "(周二)";
            case WEDNESDAY:
                return currentDate + "(周三)";
            case THURSDAY:
                return currentDate + "(周四)";
            case FRIDAY:
                return currentDate + "(周五)";
            case SATURDAY:
                return currentDate + "(周六)";
            case SUNDAY:
                return currentDate + "(周日)";
            default:
                return "";
        }

    }
}
